<?php

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

/**
 * View class
 */
class TaxiBookingViewOrder extends JView {

    function display($tpl = null) {
//		global $mainframe;
        $mainframe = JFactory::getApplication();
        //Load pane behavior
        jimport('joomla.html.pane');

        //initialise variables
        $editor = & JFactory::getEditor();
        $document = & JFactory::getDocument();
        $user = & JFactory::getUser();
        $db = & JFactory::getDBO();
        $lang = & JFactory::getLanguage();
        $pane = & JPane::getInstance('sliders');

        // Get the page/component configuration
        $params = & JComponentHelper::getParams('com_taxibooking');

        JHTML::_('behavior.tooltip');

        $nullDate = $db->getNullDate();

        //get vars
        $cid = JRequest::getVar('cid');

        //add css to document
        $document->addStyleSheet('components/com_taxibooking/assets/css/bookingbackend.css');

        //create the toolbar
        JToolBarHelper::title(JText::_('ORDER DETAILS'), 'generic');

        JToolBarHelper::save();
        JToolBarHelper::spacer();
        JToolBarHelper::cancel();

        //Get data from the model
        $model = & $this->getModel();
        $row = & $this->get('Item');
        $row_paypal = & $this->getPaypalData($row);
        $row_authnet = & $this->getAuthnetData($row);

        if (!empty($row_paypal)) {
            $row->prepay = ceil($row_paypal->mc_gross * $row->cprice / 100);
        } elseif (!empty($row_authnet)){
            $row->prepay = ceil($row_authnet->x_amount /$row->cprice * 100);
        
        }
        else{
            $row->prepay = 0;
        }

        //build selectlists
        $lists = array();

        //build state list
        $state[] = JHTML::_('select.option', 1, JText::_('Accepted'));
        $state[] = JHTML::_('select.option', 0, JText::_('Rejected'));
        $state[] = JHTML::_('select.option', -1, JText::_('Archived'));
        $state[] = JHTML::_('select.option', -2, JText::_('Waiting'));

        $lists['state'] = JHTML::_('select.genericlist', $state, 'state', '', 'value', 'text', $row->state);

        //assign data to template
        $this->assignRef('params', $params);
        $this->assignRef('lists', $lists);
        $this->assignRef('row_queue', $row);
        $this->assignRef('row_paypal', $row_paypal);
        $this->assignRef('row_authnet', $row_authnet);
        $this->assignRef('editor', $editor);
        $this->assignRef('pane', $pane);
        $this->assignRef('nullDate', $nullDate);

        parent::display($tpl);
    }

    /**
     * Method to load paypal data
     *
     * @access	private
     * @return	boolean	True on success
     * @since	1.0
     */
    function &getPaypalData($row) {
        $db = & JFactory::getDBO();
        $item = array();

        if ($row->payment == 'paypal') {
            $query = 'SELECT *'
                    . ' FROM #__taxibooking_paypal_table'
                    . ' WHERE item_number = "' . $row->confirm . '"'
            ;
            $db->setQuery($query);
            $item = $db->loadObject();
        }
        return $item;
    }

    /**
     * Method to load paypal data
     *
     * @access	private
     * @return	boolean	True on success
     * @since	1.0
     */
    function &getAuthnetData($row) {
        $db = & JFactory::getDBO();
        $item = array();

        if ($row->payment == 'authpay') {
            $query = 'SELECT *'
                    . ' FROM #__taxibooking_auth_response'
                    . ' WHERE 	order_id = "' . $row->id . '"'
            ;
            $db->setQuery($query);
            $item = $db->loadObject();
        }

        return $item;
    }

}

?>